package com.notes.designpattern.chain2;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

public class BusinHandlerChain {

    List<BusinHandler> businHandlerList = Lists.newArrayList();

    int index = 0;

    public BusinHandlerChain addBusinHandler(BusinHandler businHandler) {
        this.businHandlerList.add(businHandler);
        return this;
    }

    public Map<String, Object> process(Map<String, Object> req, BusinHandlerChain businHandlerChain) {
        if (index == businHandlerList.size()) {
            System.out.println("业务执行结束，执行成功业务数：" + index);
            Map<String, Object> resMap = Maps.newHashMap();
            resMap.put("success", "true");
            return resMap;
        }

        BusinHandler businHandler = businHandlerList.get(index);
        index++;
        Map<String, Object> processResp = businHandler.process(req, businHandlerChain);

        return processResp;
    }
}
